{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "109b9a75-ec29-43d6-9f11-68cf1813f357",
   "metadata": {},
   "source": [
    "来自书的附录1\n",
    "\n",
    "相当于整一下基础，至少要把这些式子表现出来了，后面才能用更多符号整别的东西\n",
    "电动力学这里练会了以后，其他科应该也可以照搬差不多吧。最多是变量名换一换"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3539b5b-ec11-4a9b-8ff4-6ffc410b1c36",
   "metadata": {},
   "source": [
    "先用latex格式把这些公式抄一道\n",
    "（好吧直接拍照然后simpletex了）\n",
    "\n",
    "$$\\nabla(\\varphi\\psi)=\\varphi\\nabla\\psi+\\psi\\nabla\\varphi,$$\n",
    "$$\n",
    "\\nabla\\cdot(\\varphi\\mathbf{A})=\\varphi\\nabla\\cdot\\mathbf{A}+\\mathbf{A}\\cdot\\nabla\\varphi,$$\n",
    "$$\\nabla\\times(\\varphi\\mathbf{A})=\\varphi\\nabla\\times\\mathbf{A}+\\nabla\\varphi\\times\\mathbf{A},$$\n",
    "$$\n",
    "\\nabla\\cdot(\\mathbf{A}\\times\\mathbf{B})=\\mathbf{B}\\cdot\\nabla\\times\\mathbf{A}-\\mathbf{A}\\cdot\\nabla\\times\\mathbf{B},$$\n",
    "$$\\nabla\\times(\\nabla\\times\\mathbf{A})=\\nabla(\\nabla\\cdot\\mathbf{A})-\\nabla^2\\mathbf{A},$$\n",
    "$$\n",
    "(\\nabla\\times\\mathbf{A})\\times\\mathbf{A}=\\mathbf{A}\\cdot\\nabla\\mathbf{A}-\\frac{1}{2}\\nabla\\mathbf{A}^2,$$\n",
    "$$\\nabla\\times(\\mathbf{A}\\times\\mathbf{B})=\\mathbf{A}(\\nabla\\cdot\\mathbf{B})+(\\mathbf{B}\\cdot\\nabla)\\mathbf{A}\n",
    "-\\mathbf{B}(\\nabla\\cdot\\mathbf{A})-(\\mathbf{A}\\cdot\\nabla)\\mathbf{B},$$\n",
    "$$\\nabla(\\mathbf{A}\\cdot\\mathbf{B})=(\\mathbf{A}\\cdot\\nabla)\\mathbf{B}+(\\mathbf{B}\\cdot\\nabla)\\mathbf{A}\n",
    "+\\mathbf{A}\\times(\\nabla\\times\\mathbf{B})+\\mathbf{B}\\times(\\nabla\\times\\mathbf{A}).$$\n",
    "$$\\nabla r=\\frac{\\mathbf{r}}{r}=\\mathbf{e}(r),$$\n",
    "$$\n",
    "\\nabla f(r)=\\frac{df}{dr}\\mathbf{e}(r),$$\n",
    "$$\\nabla^2\\frac{1}{r}=-4\\pi\\delta(r).$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cd5803c7-206a-45e4-b9ff-e6ba25e50d52",
   "metadata": {},
   "source": [
    "先定义总体的环境，然后一个一个来\n",
    "\n",
    "（话说这些是不是可以用deepseek）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80c771cd-802b-4e43-a851-512fcf125ed2",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "**解释：**\n",
    "\n",
    "1. **导入模块和定义空间：** 使用`EuclideanSpace`创建三维空间，并定义笛卡尔坐标系。\n",
    "2. **标量场和向量场：** 使用`scalar_field`和`vector_field`定义符号场φ, ψ, A, B。\n",
    "3. **位置向量相关：** 定义标量场`r`为位置向量的模，向量场`r_vec`为位置向量。\n",
    "4. **等式定义：** 使用梯度`.gradient()`、散度`.div()`、旋度`.curl()`和拉普拉斯`.laplacian()`方法，以及点乘`.dot()`和叉乘`.cross()`运算符，将各个矢量恒等式转换为SageMath符号表达式。\n",
    "5. **Dirac δ函数处理：** 最后一条等式涉及δ函数，SageMath中可能无法直接验证，故符号性表示。\n",
    "\n",
    "**注意：** 部分等式（如涉及δ函数的）可能需要进一步调整以适应SageMath的符号处理能力，上述代码主要展示符号表达式的构建方式。实际运算时，部分等式可能需要具体展开或使用其他方法验证。"
   ]
  },
  {
   "cell_type": "raw",
   "id": "332e28da-480f-4b65-8487-3f2640bb9e29",
   "metadata": {},
   "source": [
    "# 以下是用SageMath符号语言表示这些矢量分析公式的代码：\n",
    "from sage.manifolds import *\n",
    "from sage.calculus.var import var\n",
    "#from sage.functions.delta import delta\n",
    "\n",
    "# 定义三维欧几里得空间和笛卡尔坐标系\n",
    "E.<x,y,z> = EuclideanSpace()\n",
    "cartesian = E.default_chart()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "5bbaf624-e089-49a1-9c28-fcfe8b418860",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[0;31mSignature:\u001b[0m     \n",
       "\u001b[0mE\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mscalar_field\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m    \u001b[0mcoord_expression\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m    \u001b[0mchart\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m    \u001b[0mname\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m    \u001b[0mlatex_name\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0;34m'ScalarField'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
       "\u001b[0;31mDocstring:\u001b[0m     \n",
       "   Define a scalar field on the manifold.\n",
       "\n",
       "   See \"ScalarField\" (or \"DiffScalarField\" if the manifold is\n",
       "   differentiable) for a complete documentation.\n",
       "\n",
       "   INPUT:\n",
       "\n",
       "   * \"coord_expression\" -- (default: \"None\") coordinate expression(s)\n",
       "     of the scalar field; this can be either\n",
       "\n",
       "     * a single coordinate expression; if the argument \"chart\" is\n",
       "       \"'all'\", this expression is set to all the charts defined on\n",
       "       the open set; otherwise, the expression is set in the specific\n",
       "       chart provided by the argument \"chart\"\n",
       "\n",
       "     * a dictionary of coordinate expressions, with the charts as keys\n",
       "\n",
       "   * \"chart\" -- (default: \"None\") chart defining the coordinates used\n",
       "     in \"coord_expression\" when the latter is a single coordinate\n",
       "     expression; if \"None\", the default chart of the open set is\n",
       "     assumed; if \"chart=='all'\", \"coord_expression\" is assumed to be\n",
       "     independent of the chart (constant scalar field)\n",
       "\n",
       "   * \"name\" -- (default: \"None\") name given to the scalar field\n",
       "\n",
       "   * \"latex_name\" -- (default: \"None\") LaTeX symbol to denote the\n",
       "     scalar field; if \"None\", the LaTeX symbol is set to \"name\"\n",
       "\n",
       "   If \"coord_expression\" is \"None\" or does not fully specified the\n",
       "   scalar field, other coordinate expressions can be added\n",
       "   subsequently by means of the methods \"add_expr()\",\n",
       "   \"add_expr_by_continuation()\", or \"set_expr()\"\n",
       "\n",
       "   OUTPUT:\n",
       "\n",
       "   * instance of \"ScalarField\" (or of the subclass \"DiffScalarField\"\n",
       "     if the manifold is differentiable) representing the defined\n",
       "     scalar field\n",
       "\n",
       "   EXAMPLES:\n",
       "\n",
       "   A scalar field defined by its coordinate expression in the open\n",
       "   set's default chart:\n",
       "\n",
       "      sage: M = Manifold(3, 'M', structure='topological')\n",
       "      sage: U = M.open_subset('U')\n",
       "      sage: c_xyz.<x,y,z> = U.chart()\n",
       "      sage: f = U.scalar_field(sin(x)*cos(y) + z, name='F'); f\n",
       "      Scalar field F on the Open subset U of the 3-dimensional topological manifold M\n",
       "      sage: f.display()\n",
       "      F: U → ℝ\n",
       "         (x, y, z) ↦ cos(y)*sin(x) + z\n",
       "      sage: f.parent()\n",
       "      Algebra of scalar fields on the Open subset U of the 3-dimensional topological manifold M\n",
       "      sage: f in U.scalar_field_algebra()\n",
       "      True\n",
       "\n",
       "   Equivalent definition with the chart specified:\n",
       "\n",
       "      sage: f = U.scalar_field(sin(x)*cos(y) + z, chart=c_xyz, name='F')\n",
       "      sage: f.display()\n",
       "      F: U → ℝ\n",
       "         (x, y, z) ↦ cos(y)*sin(x) + z\n",
       "\n",
       "   Equivalent definition with a dictionary of coordinate\n",
       "   expression(s):\n",
       "\n",
       "      sage: f = U.scalar_field({c_xyz: sin(x)*cos(y) + z}, name='F')\n",
       "      sage: f.display()\n",
       "      F: U → ℝ\n",
       "         (x, y, z) ↦ cos(y)*sin(x) + z\n",
       "\n",
       "   See the documentation of class \"ScalarField\" for more examples.\n",
       "\n",
       "   See also:\n",
       "\n",
       "     \"constant_scalar_field()\", \"zero_scalar_field()\",\n",
       "     \"one_scalar_field()\"\n",
       "\u001b[0;31mInit docstring:\u001b[0m Initialize self.  See help(type(self)) for accurate signature.\n",
       "\u001b[0;31mFile:\u001b[0m           /usr/lib/python3.12/site-packages/sage/manifolds/manifold.py\n",
       "\u001b[0;31mType:\u001b[0m           method"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "E.scalar_field?\n",
    "# scalar: 标量\n",
    "#所以这个是标量场"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "56593239-ba29-4855-953d-888b9dc39c2d",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[0;31mSignature:\u001b[0m      \u001b[0mE\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvector_field\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mcomp\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
       "\u001b[0;31mDocstring:\u001b[0m     \n",
       "   Define a vector field on \"self\".\n",
       "\n",
       "   Via the argument \"dest_map\", it is possible to let the vector field\n",
       "   take its values on another manifold. More precisely, if M is the\n",
       "   current manifold, N a differentiable manifold and \\Phi:\\  M\n",
       "   --> N a differentiable map, a *vector field along* M *with\n",
       "   values on* N is a differentiable map\n",
       "\n",
       "      v:\\ M  ---> TN\n",
       "\n",
       "   (TN being the tangent bundle of N) such that\n",
       "\n",
       "      \\forall p \\in M,\\ v(p) \\in T_{\\Phi(p)} N,\n",
       "\n",
       "   where T_{\\Phi(p)} N is the tangent space to N at the point \\Phi(p).\n",
       "\n",
       "   The standard case of vector fields *on* M corresponds to N = M and\n",
       "   \\Phi = Id_M. Other common cases are \\Phi being an immersion and\n",
       "   \\Phi being a curve in N (M is then an open interval of \\RR).\n",
       "\n",
       "   See also:\n",
       "\n",
       "     \"VectorField\" and \"VectorFieldParal\" for a complete\n",
       "     documentation.\n",
       "\n",
       "   INPUT:\n",
       "\n",
       "   * \"comp\" -- (optional) either the components of the vector field\n",
       "     with respect to the vector frame specified by the argument\n",
       "     \"frame\" or a dictionary of components, the keys of which are\n",
       "     vector frames or pairs \"(f, c)\" where \"f\" is a vector frame and\n",
       "     \"c\" the chart in which the components are expressed\n",
       "\n",
       "   * \"frame\" -- (default: \"None\"; unused if \"comp\" is not given or is\n",
       "     a dictionary) vector frame in which the components are given; if\n",
       "     \"None\", the default vector frame of \"self\" is assumed\n",
       "\n",
       "   * \"chart\" -- (default: \"None\"; unused if \"comp\" is not given or is\n",
       "     a dictionary) coordinate chart in which the components are\n",
       "     expressed; if \"None\", the default chart on the domain of \"frame\"\n",
       "     is assumed\n",
       "\n",
       "   * \"name\" -- (default: \"None\") name given to the vector field\n",
       "\n",
       "   * \"latex_name\" -- (default: \"None\") LaTeX symbol to denote the\n",
       "     vector field; if none is provided, the LaTeX symbol is set to\n",
       "     \"name\"\n",
       "\n",
       "   * \"dest_map\" -- (default: \"None\") the destination map \\Phi:\\ M\n",
       "     --> N; if \"None\", it is assumed that N = M and that \\Phi\n",
       "     is the identity map (case of a vector field *on* M), otherwise\n",
       "     \"dest_map\" must be a \"DiffMap\"\n",
       "\n",
       "   OUTPUT:\n",
       "\n",
       "   * a \"VectorField\" (or if N is parallelizable, a \"VectorFieldParal\")\n",
       "     representing the defined vector field\n",
       "\n",
       "   EXAMPLES:\n",
       "\n",
       "   A vector field on a open subset of a 3-dimensional differentiable\n",
       "   manifold:\n",
       "\n",
       "      sage: M = Manifold(3, 'M')\n",
       "      sage: U = M.open_subset('U')\n",
       "      sage: c_xyz.<x,y,z> = U.chart()\n",
       "      sage: v = U.vector_field(y, -x*z, 1+y, name='v'); v\n",
       "      Vector field v on the Open subset U of the 3-dimensional\n",
       "       differentiable manifold M\n",
       "      sage: v.display()\n",
       "      v = y ∂/∂x - x*z ∂/∂y + (y + 1) ∂/∂z\n",
       "\n",
       "   The vector fields on U form the set \\mathfrak{X}(U), which is a\n",
       "   module over the algebra C^k(U) of differentiable scalar fields on\n",
       "   U:\n",
       "\n",
       "      sage: v.parent()\n",
       "      Free module X(U) of vector fields on the Open subset U of the\n",
       "       3-dimensional differentiable manifold M\n",
       "      sage: v in U.vector_field_module()\n",
       "      True\n",
       "\n",
       "   For more examples, see \"VectorField\" and \"VectorFieldParal\".\n",
       "\u001b[0;31mInit docstring:\u001b[0m Initialize self.  See help(type(self)) for accurate signature.\n",
       "\u001b[0;31mFile:\u001b[0m           /usr/lib/python3.12/site-packages/sage/manifolds/differentiable/manifold.py\n",
       "\u001b[0;31mType:\u001b[0m           method"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "E.vector_field?\n",
    "# 矢量场"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a030dcdf-0fad-4a50-99e2-84afcf831d02",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sage.manifolds import *\n",
    "from sage.calculus.var import var\n",
    "\n",
    "# 定义三维欧几里得空间和笛卡尔坐标系\n",
    "E.<x,y,z> = EuclideanSpace()\n",
    "cartesian = E.default_chart()\n",
    "\n",
    "# 定义标量场φ, ψ和向量场A, B\n",
    "phi = E.scalar_field(function('phi')(x,y,z), name='φ')\n",
    "psi = E.scalar_field(function('psi')(x,y,z), name='ψ')\n",
    "\n",
    "# 修正向量场A, B的定义语法\n",
    "A_x = function('A_x')(x,y,z)\n",
    "A_y = function('A_y')(x,y,z)\n",
    "A_z = function('A_z')(x,y,z)\n",
    "A = E.vector_field(A_x, A_y, A_z, name='A')\n",
    "\n",
    "B_x = function('B_x')(x,y,z)\n",
    "B_y = function('B_y')(x,y,z)\n",
    "B_z = function('B_z')(x,y,z)\n",
    "B = E.vector_field(B_x, B_y, B_z, name='B')\n",
    "\n",
    "# 定义位置向量r的模和单位向量\n",
    "r = E.scalar_field(sqrt(x^2 + y^2 + z^2), name='r')\n",
    "r_vec = E.vector_field([x, y, z], name='r_vec')\n",
    "e_r = r_vec / r  # 单位径向向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "642e766a-5860-4e91-aea9-0befd9055ff4",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'VectorFieldFreeModule_with_category.element_class' object has no attribute 'gradient'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[6], line 7\u001b[0m\n\u001b[1;32m      5\u001b[0m eq4 \u001b[38;5;241m=\u001b[39m (A\u001b[38;5;241m.\u001b[39mcross(B))\u001b[38;5;241m.\u001b[39mdivergence() \u001b[38;5;241m==\u001b[39m B\u001b[38;5;241m.\u001b[39mdot(A\u001b[38;5;241m.\u001b[39mcurl()) \u001b[38;5;241m-\u001b[39m A\u001b[38;5;241m.\u001b[39mdot(B\u001b[38;5;241m.\u001b[39mcurl())\n\u001b[1;32m      6\u001b[0m eq5 \u001b[38;5;241m=\u001b[39m (A\u001b[38;5;241m.\u001b[39mcurl())\u001b[38;5;241m.\u001b[39mcurl() \u001b[38;5;241m==\u001b[39m (A\u001b[38;5;241m.\u001b[39mdivergence())\u001b[38;5;241m.\u001b[39mgradient() \u001b[38;5;241m-\u001b[39m A\u001b[38;5;241m.\u001b[39mlaplacian()\n\u001b[0;32m----> 7\u001b[0m eq6 \u001b[38;5;241m=\u001b[39m (A\u001b[38;5;241m.\u001b[39mcurl())\u001b[38;5;241m.\u001b[39mcross(A) \u001b[38;5;241m==\u001b[39m A\u001b[38;5;241m.\u001b[39mdot(\u001b[43mA\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgradient\u001b[49m()) \u001b[38;5;241m-\u001b[39m RealNumber(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m0.5\u001b[39m\u001b[38;5;124m'\u001b[39m) \u001b[38;5;241m*\u001b[39m (A\u001b[38;5;241m.\u001b[39mdot(A))\u001b[38;5;241m.\u001b[39mgradient()\n\u001b[1;32m      8\u001b[0m eq7 \u001b[38;5;241m=\u001b[39m (A\u001b[38;5;241m.\u001b[39mcross(B))\u001b[38;5;241m.\u001b[39mcurl() \u001b[38;5;241m==\u001b[39m A \u001b[38;5;241m*\u001b[39m B\u001b[38;5;241m.\u001b[39mdivergence() \u001b[38;5;241m+\u001b[39m (B\u001b[38;5;241m.\u001b[39mdot(A\u001b[38;5;241m.\u001b[39mgradient())) \u001b[38;5;241m-\u001b[39m B \u001b[38;5;241m*\u001b[39m A\u001b[38;5;241m.\u001b[39mdivergence() \u001b[38;5;241m-\u001b[39m (A\u001b[38;5;241m.\u001b[39mdot(B\u001b[38;5;241m.\u001b[39mgradient()))\n\u001b[1;32m      9\u001b[0m eq8 \u001b[38;5;241m=\u001b[39m (A\u001b[38;5;241m.\u001b[39mdot(B))\u001b[38;5;241m.\u001b[39mgradient() \u001b[38;5;241m==\u001b[39m A\u001b[38;5;241m.\u001b[39mdot(B\u001b[38;5;241m.\u001b[39mgradient()) \u001b[38;5;241m+\u001b[39m B\u001b[38;5;241m.\u001b[39mdot(A\u001b[38;5;241m.\u001b[39mgradient()) \u001b[38;5;241m+\u001b[39m A\u001b[38;5;241m.\u001b[39mcross(B\u001b[38;5;241m.\u001b[39mcurl()) \u001b[38;5;241m+\u001b[39m B\u001b[38;5;241m.\u001b[39mcross(A\u001b[38;5;241m.\u001b[39mcurl())\n",
      "File \u001b[0;32m/usr/lib/python3.12/site-packages/sage/structure/element.pyx:495\u001b[0m, in \u001b[0;36msage.structure.element.Element.__getattr__ (build/cythonized/sage/structure/element.c:11829)\u001b[0;34m()\u001b[0m\n\u001b[1;32m    493\u001b[0m         AttributeError: 'LeftZeroSemigroup_with_category.element_class' object has no attribute 'blah_blah'...\n\u001b[1;32m    494\u001b[0m     \"\"\"\n\u001b[0;32m--> 495\u001b[0m     return self.getattr_from_category(name)\n\u001b[1;32m    496\u001b[0m \n\u001b[1;32m    497\u001b[0m cdef getattr_from_category(self, name):\n",
      "File \u001b[0;32m/usr/lib/python3.12/site-packages/sage/structure/element.pyx:508\u001b[0m, in \u001b[0;36msage.structure.element.Element.getattr_from_category (build/cythonized/sage/structure/element.c:11939)\u001b[0;34m()\u001b[0m\n\u001b[1;32m    506\u001b[0m     else:\n\u001b[1;32m    507\u001b[0m         cls = P._abstract_element_class\n\u001b[0;32m--> 508\u001b[0m     return getattr_from_other_class(self, cls, name)\n\u001b[1;32m    509\u001b[0m \n\u001b[1;32m    510\u001b[0m def __dir__(self):\n",
      "File \u001b[0;32m/usr/lib/python3.12/site-packages/sage/cpython/getattr.pyx:358\u001b[0m, in \u001b[0;36msage.cpython.getattr.getattr_from_other_class (build/cythonized/sage/cpython/getattr.c:4392)\u001b[0;34m()\u001b[0m\n\u001b[1;32m    356\u001b[0m     dummy_error_message.cls = type(self)\n\u001b[1;32m    357\u001b[0m     dummy_error_message.name = name\n\u001b[0;32m--> 358\u001b[0m     raise AttributeError(dummy_error_message)\n\u001b[1;32m    359\u001b[0m cdef PyObject* attr = instance_getattr(cls, name)\n\u001b[1;32m    360\u001b[0m if attr is NULL:\n",
      "\u001b[0;31mAttributeError\u001b[0m: 'VectorFieldFreeModule_with_category.element_class' object has no attribute 'gradient'"
     ]
    }
   ],
   "source": [
    "# 梯度、散度、旋度等式（修正后的语法）\n",
    "eq1 = (phi * psi).gradient() == phi * psi.gradient() + psi * phi.gradient()\n",
    "eq2 = (phi * A).divergence() == phi * A.divergence() + A.dot(phi.gradient())\n",
    "eq3 = (phi * A).curl() == phi * A.curl() + phi.gradient().cross(A)\n",
    "eq4 = (A.cross(B)).divergence() == B.dot(A.curl()) - A.dot(B.curl())\n",
    "eq5 = (A.curl()).curl() == (A.divergence()).gradient() - A.laplacian()\n",
    "eq6 = (A.curl()).cross(A) == A.dot(A.gradient()) - 0.5 * (A.dot(A)).gradient()\n",
    "eq7 = (A.cross(B)).curl() == A * B.divergence() + (B.dot(A.gradient())) - B * A.divergence() - (A.dot(B.gradient()))\n",
    "eq8 = (A.dot(B)).gradient() == A.dot(B.gradient()) + B.dot(A.gradient()) + A.cross(B.curl()) + B.cross(A.curl())\n",
    "eq9 = r.gradient() == e_r\n",
    "eq10 = (r.gradient()).expr() == diff(r.expr(), r) * e_r  # 假设f(r)的梯度形式\n",
    "eq11 = (1/r).laplacian() == -4 * pi * DiracDelta(x,y,z)  # 符号性表示（需额外定义）\n",
    "\n",
    "# 显示所有等式\n",
    "eq1.show()\n",
    "eq2.show()\n",
    "eq3.show()\n",
    "eq4.show()\n",
    "eq5.show()\n",
    "eq6.show()\n",
    "eq7.show()\n",
    "eq8.show()\n",
    "eq9.show()\n",
    "eq10.show()\n",
    "eq11.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4764466c-05f8-40f9-9be3-e2e3ed9ddf14",
   "metadata": {},
   "source": [
    "$$\\nabla(\\varphi\\psi)=\\varphi\\nabla\\psi+\\psi\\nabla\\varphi,$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ab80ea6c-a670-4329-b906-4089bb014f1d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "b6773e9b-5aa5-4a3a-a11b-367532febace",
   "metadata": {},
   "source": [
    "$$\n",
    "\\nabla\\cdot(\\varphi\\mathbf{A})=\\varphi\\nabla\\cdot\\mathbf{A}+\\mathbf{A}\\cdot\\nabla\\varphi,$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "08e32cfa-2132-4fef-bcc3-ae3c4c0c29ec",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "40bc65de-8972-4d9d-b8db-ed3e468c4994",
   "metadata": {},
   "source": [
    "$$\\nabla\\times(\\varphi\\mathbf{A})=\\varphi\\nabla\\times\\mathbf{A}+\\nabla\\varphi\\times\\mathbf{A},$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4429eb2f-1cd9-44ca-8474-dc457820ae0c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "a298ebdf-7574-475b-b7a4-45c66283addd",
   "metadata": {},
   "source": [
    "$$\n",
    "\\nabla\\cdot(\\mathbf{A}\\times\\mathbf{B})=\\mathbf{B}\\cdot\\nabla\\times\\mathbf{A}-\\mathbf{A}\\cdot\\nabla\\times\\mathbf{B},$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "175ec1b3-6656-4513-82d7-07c79f818a4c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "0e1f6468-f3de-4945-863a-c44594c0026a",
   "metadata": {},
   "source": [
    "$$\\nabla\\times(\\nabla\\times\\mathbf{A})=\\nabla(\\nabla\\cdot\\mathbf{A})-\\nabla^2\\mathbf{A},$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "52374f2d-8481-4624-b690-407969e75d59",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "1e1cb587-51b6-4100-919a-df0d371ee87d",
   "metadata": {},
   "source": [
    "$$\n",
    "(\\nabla\\times\\mathbf{A})\\times\\mathbf{A}=\\mathbf{A}\\cdot\\nabla\\mathbf{A}-\\frac{1}{2}\\nabla\\mathbf{A}^2,$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7ab25f63-454d-4f14-8787-8bfa8e380639",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "6d10f0ec-848d-4b98-98bc-1b923b891512",
   "metadata": {},
   "source": [
    "$$\\nabla\\times(\\mathbf{A}\\times\\mathbf{B})=\\mathbf{A}(\\nabla\\cdot\\mathbf{B})+(\\mathbf{B}\\cdot\\nabla)\\mathbf{A}\n",
    "-\\mathbf{B}(\\nabla\\cdot\\mathbf{A})-(\\mathbf{A}\\cdot\\nabla)\\mathbf{B},$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a4c6a7c5-c2d4-4849-a38d-44e1554b6a03",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "5fbca8f1-a462-4ce8-a14a-3c17d48c9176",
   "metadata": {},
   "source": [
    "$$\\nabla(\\mathbf{A}\\cdot\\mathbf{B})=(\\mathbf{A}\\cdot\\nabla)\\mathbf{B}+(\\mathbf{B}\\cdot\\nabla)\\mathbf{A}\n",
    "+\\mathbf{A}\\times(\\nabla\\times\\mathbf{B})+\\mathbf{B}\\times(\\nabla\\times\\mathbf{A}).$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c3d62951-c910-428e-b9f7-e573b445b5ea",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "6343830d-a1a5-4759-985f-68a97ba28194",
   "metadata": {},
   "source": [
    "$$\\nabla r=\\frac{\\mathbf{r}}{r}=\\mathbf{e}(r),$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d9128d8-5395-4e4a-9bbd-354f28aaf7b4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "a2b6e885-5941-43ed-8d24-6fe22b820b99",
   "metadata": {},
   "source": [
    "$$\n",
    "\\nabla f(r)=\\frac{df}{dr}\\mathbf{e}(r),$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e217e265-6d75-4229-bcd7-03c2bfa8da15",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "4566b850-4936-457a-bad5-badb4cedf4a1",
   "metadata": {},
   "source": [
    "$$\\nabla^2\\frac{1}{r}=-4\\pi\\delta(r).$$"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "SageMath 10.5",
   "language": "sage",
   "name": "sagemath"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
